﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Gencode.VECore;
using WF.VecDraw.Dialog.Controls;

namespace WF.VecDraw.Dialog
{
    /// <summary>
    /// Interaction logic for LayerDialog.xaml
    /// </summary>
    public partial class LayerDialog : Window
    {
        CanvasDocument _canvas;

        public LayerDialog(CanvasDocument canvas)
        {
            InitializeComponent();

            _canvas = canvas;

            Clear();
            foreach (CanvasLayer layer in _canvas.Layers)
            {
                AddLayer(layer);
            }
        }

        private void AddLayer(CanvasLayer layer)
        {
            var l = new LayerDisplay(layer);
            layerPanel.Children.Insert(0, l);

            l.LayerGetFocus += new LayerFocusChangedEventHandler(LayerFocusChanged);
            l.Selected = true;
        }

        private void LayerFocusChanged(object sender, LayerFocusChangedEventArgs e)
        {
            foreach (LayerDisplay layer in layerPanel.Children)
            {
                if (layer != sender)
                    layer.Selected = false;
            }

            _canvas.SelectedLayer = e.Layer;
        }

        /// <summary>
        /// clears all displayed layers
        /// </summary>
        public void Clear()
        {
            layerPanel.Children.Clear();
        }

        private void StatusBar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _canvas.AddLayer();
            AddLayer(_canvas.SelectedLayer);
        }
    }
}
